package com.project.ssm.dao;

import com.project.ssm.domain.Permission;
import com.project.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRoleDao {
    //根据用户id查询角色
    @Select("select * from role where id in (select roleId from user_role where userId = #{userId})")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.project.ssm.dao.IPermissionDao.findPermissionByRoleId"))
    })
    public List<Role> findRoleByUserId(Integer userId) throws Exception;

    @Select("select * from role")
    public List<Role> findAll() throws Exception;

    @Select("select * from role where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.project.ssm.dao.IPermissionDao.findPermissionByRoleId"))
    })
    public Role findById(Integer id) throws Exception;

    @Insert("insert into role values(NULL, #{roleName}, #{roleDesc})")
    public void save(Role role) throws Exception;

    @Delete("delete from role where id = #{id}")
    public void deleteRole(Integer id) throws Exception;

    @Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{id})")
    public List<Permission> findOtherPermission(Integer id) throws Exception;

    @Insert("insert into role_permission values(#{roleId}, #{permissionId})")
    public void addPermissionToRole(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId) throws Exception;
}
